var XMLHttpReq = false;

function $(id)
{
    return document.getElementById(id);
}

/**20170827
 * 增加了obj = new XMLHttpRequest();  
 * 用于支持IE7及以上、EDGE等浏览器
 * @returns
 */
function getXmlHttp()
{
	var prefix = ["Msxml3.XMLHTTP","Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
				  "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP.2.6",
				  "Microsoft.XMLHTTP.1.0","Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
	for (var i=0;i<prefix.length;i++) {
		try {
			var obj = new ActiveXObject(prefix[i]);
			//var obj = getActiveXObject(prefix[i]);
			if (obj) return obj;
		} catch (E) {}
	}
	if (!obj){
		obj = new XMLHttpRequest();  
		if (obj) return obj;
		else alert("无法创建 XMLHttpRequest 对象！");
	}
}



function createXMLHttpRequest() 
{
	if (window.XMLHttpRequest){
		XMLHttpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
} 

/**
 * 简单的调用，采用'GET'方式，异步与后台通讯。
 * @param url : 通讯地址；
 * @param callback：回调函数；
 */
function sendRequest(url,callback) 
{
	var xh = getXmlHttp();
	var para = '';
	if (url.indexOf("?")>0){
		para='&random=' + Math.random();
	}else {
		para='?random=' + Math.random();
	}
	xh.open('GET', url, true);
	xh.setRequestHeader("Content-Type","UTF-8");
    xh.onreadystatechange = function(){if (4 == xh.readyState){callback(xh);}}
	xh.send(null);
}


/**
 * 较复杂的调用
 * @param action : 提交方式，包含'GET'和'POST'，'GET'方式只能传输2046个字节，但传输速度快；
 * @param async : Boolean变量，true表示使用异步方式通讯，false表示同步方式通讯；
 * @param url : 通讯地址；
 * @param callback：回调函数；
 * @return : 返回callback的返回值；
 */
function AjaxConnect(action, async, url, callback) 
{
	var xh = getXmlHttp();
	var para = '';
	if (url.indexOf("?")>0){
		para='&random=' + Math.random();
	}else {
		para='?random=' + Math.random();
	}
	xh.open(action, url, async);
	xh.setRequestHeader("Content-Type","UTF-8");
    if (async) xh.onreadystatechange = function(){if (4 == xh.readyState){callback(xh);}}
	xh.send(null);
	if (!async) {
		var result = xh.status;		   
	    //OK
	    if(result==200)
	    {
			return callback(xh);
	    }
	}
}




function sendReq(url)
{
	createXMLHttpRequest();
	XMLHttpReq.open("GET",url,true);
	XMLHttpReq.onreadystatechange = processResponse;
	XMLHttpReq.send(null); 	
}


function processResponse()
{
	if (XMLHttpReq.readyState == 4) {
		if (XMLHttpReq.status == 200) {
			var res = XMLHttpReq.responseText;
			document.write(res);
			//document.getElementById('id').innerHTML = res;
		} else {
			window.alert("There are some Exp on this page !");
		}
	}
}


function callbackExample(xh)
{
	var str = xh.responseText;
	if (xh.responseText=='0')
    {
		window.alert("111111");
    }else{
		window.alert("222222");
	}
}